﻿
using System.ServiceModel.DomainServices.Server;

namespace MVVMBasicRIALibrary.Web.Services
{
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using DataModel;


    // Implements application logic using the MVVMBasicEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class BasicDomainService : LinqToEntitiesDomainService<MVVMBasicEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Addresses' query.
        public IQueryable<Address> GetAddresses()
        {
            return this.ObjectContext.Addresses;
        }

        public void InsertAddress(Address address)
        {
            if ((address.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(address, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Addresses.AddObject(address);
            }
        }

        public void UpdateAddress(Address currentAddress)
        {
            this.ObjectContext.Addresses.AttachAsModified(currentAddress, this.ChangeSet.GetOriginal(currentAddress));
        }

        public void DeleteAddress(Address address)
        {
            if ((address.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Addresses.Attach(address);
            }
            this.ObjectContext.Addresses.DeleteObject(address);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Contacts' query.
        public IQueryable<Contact> GetContacts()
        {
            return this.ObjectContext.Contacts;
        }

        public void InsertContact(Contact contact)
        {
            if ((contact.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(contact, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Contacts.AddObject(contact);
            }
        }

        public void UpdateContact(Contact currentContact)
        {
            this.ObjectContext.Contacts.AttachAsModified(currentContact, this.ChangeSet.GetOriginal(currentContact));
        }

        public void DeleteContact(Contact contact)
        {
            if ((contact.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Contacts.Attach(contact);
            }
            this.ObjectContext.Contacts.DeleteObject(contact);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Dictionaries' query.
        public IQueryable<Dictionary> GetDictionaries()
        {
            return this.ObjectContext.Dictionaries;
        }

        public void InsertDictionary(Dictionary dictionary)
        {
            if ((dictionary.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(dictionary, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Dictionaries.AddObject(dictionary);
            }
        }

        public void UpdateDictionary(Dictionary currentDictionary)
        {
            this.ObjectContext.Dictionaries.AttachAsModified(currentDictionary, this.ChangeSet.GetOriginal(currentDictionary));
        }

        [RequiresAuthentication]
        public void DeleteDictionary(Dictionary dictionary)
        {
            if ((dictionary.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Dictionaries.Attach(dictionary);
            }
            this.ObjectContext.Dictionaries.DeleteObject(dictionary);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Employees' query.
        public IQueryable<Employee> GetEmployees()
        {
            return this.ObjectContext.Employees;
        }

        public void InsertEmployee(Employee employee)
        {
            if ((employee.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Employees.AddObject(employee);
            }
        }

        public void UpdateEmployee(Employee currentEmployee)
        {
            this.ObjectContext.Employees.AttachAsModified(currentEmployee, this.ChangeSet.GetOriginal(currentEmployee));
        }

        public void DeleteEmployee(Employee employee)
        {
            if ((employee.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Employees.Attach(employee);
            }
            this.ObjectContext.Employees.DeleteObject(employee);
        }
    }
}


